package logic;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * this class is a representation of a hunter. 
 * a hunter can't die.
 * @author Vossen en Konijnen group 2
 */

public class Hunter implements Actor 
{
    // Whether the hunter is alive or not.
    private boolean alive;
	// The hunter's field.
    private Field field;
    // The hunter's position in the field.
    private Location location;
    //random
    private Random random;
   //trapchance
    private static int trapChance = 500;
    
	/**
	 * Create a hunter.  
     * @param field The field currently occupied.
     * @param location The location within the field.
	 */
    public Hunter(Field field, Location location) 
	{
        random=new Random();
    	alive = true;
        this.field = field;
        setLocation(location);
	}

	@Override
	public void act(List<Actor> newActors) 
	{
        Location oldLocation = getLocation();
        for(int i=0; i < 5; i++)
        {
        	int traps = getRandomValue(0, trapChance);
        	Location newLocation = findGain(location);
        	if(newLocation == null) { 
        		// No gain found - try to move to a free location.
        		newLocation = getField().freeAdjacentLocation(location);
        	}
        	// See if it was possible to move.
        	if(newLocation != null) {
        		setLocation(newLocation);
        		 if(traps == trapChance){
        			 new Landmine(field, oldLocation);
        		 }
        			 
        	}
        }
	}
	
	/**
	 * looks around the current location for gain. 
	 * @param location the current location
	 * @return the location of the possible gain.
	 */
	private Location findGain(Location location)
	    {
	        Field field = getField();
	        List<Location> adjacent = field.adjacentLocations(getLocation());
	        Iterator<Location> it = adjacent.iterator();
	        while(it.hasNext()) {
	            Location where = it.next();
	            Object actor = field.getObjectAt(where);
	            if(actor instanceof Rabbit) {
	                Rabbit rabbit = (Rabbit) actor;
	                if(rabbit.isAlive()) { 
	                    rabbit.setDead();
	                    // Remove the dead rabbit from the field.
	                    return where;
	                }
	            }
	            else if(actor instanceof Duck){
	            	Duck duck = (Duck) actor;
	            	if(duck.isAlive()) {
	            	duck.setDead();
                    // Remove the dead duck from the field.
                    return where;
	            	}
	            }
	            else if(actor instanceof Fox)
	            {
	            	Fox fox = (Fox) actor;
	            	
	            		if(fox.isAlive()){
	            			fox.setDead();
	                        // Remove the dead duck from the field.
	                        return where;
	            		}
	            }
	            else if(actor instanceof Grass)
	            {
	            	Grass grass = (Grass) actor;
	            	if(grass.isAlive())
	            	{
	            		return where;
	            	}
	            }
	            
	            
	        }
	        return null;
	    }
	
    /**
     * Place the hunter at the new location in the given field.
     * @param newLocation The hunters new location.
     */
	@Override
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }

	@Override
	public boolean isActive() 
	{
		return alive;
	}

	@Override
	public Location getLocation() 
	{
		return location;
	}

	@Override
	public Field getField() 
	{
		return field;
		
	}
	
	/**
	 * Sets the hunter dead.
	 */
	public void setDead()
	{
		alive=false;
	}
	
	/**
	 * Give a random number between the min and max
	 * @param min
	 * @param max
	 * @return random number
	 */
	private int getRandomValue(int min, int max)
    {
        return random.nextInt(max-min+1)+min;
    }
	
	/**
	 * Return the chance of getting trapped.
	 * @return trapChance
	 */
	public static int getTrapChance()
	{
		return trapChance;
	}
	
	/**
	 * sets the chance of getting trapped.
	 * @param tChance
	 */
	public static void setTrapChance(int tChance)
	{
		trapChance=tChance;
	}

}
